package com.wang.jmonkey.common.support.yml;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * @Description 将yml配置文件解析成properties格式
 * @Author HeJiawang
 * @Date 2021/5/8 9:42
 */
public class TransformYmlFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {

        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();

        factory.setResources(resource.getResource());
        Properties prop = factory.getObject();
        return (name != null ? new PropertiesPropertySource(name, prop) : new PropertiesPropertySource(resource.getResource().getFilename() , prop));
    }

}
